﻿using System;
using System.Collections;
using System.Collections.Generic;
using CXedu.Model;
using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;

namespace CXedu.Business
{
    public interface ICategoryBusinessFactory
    {
        ICategoryBusiness CreateCategoryBusiness();
    }
    public class CategoryBusinessFactory: BusinessFactory, ICategoryBusinessFactory
    {
        public ICategoryBusiness CreateCategoryBusiness()
        {
            return new Default.CategoryBusiness();
        }    
    }
    ///<summary>
    /// interface
    ///</summary>
    public interface ICategoryBusiness : IBusinessBase<Category, int?>
    {
        ///<summary>
        ///根据ID值读取Category实体
        ///</summary>
        Category GetById(int? id);
        ///<summary>
        ///根据Category实体插入数据库
        ///</summary>
        object Insert(Category entity);
        ///<summary>
        ///根据Category实体更新数据库
        ///</summary>
        int Update(Category entity);
        ///<summary>
		///<summary>
        ///根据HashTable更新数据库(只更新指定字段)
        ///</summary>
        int UpdateSomeVal(Hashtable table);        
        ///<summary>
        ///根据Category实体删除数据库
        ///</summary>
        int Delete(Category entity);
        ///<summary>
        ///根据Category实体ID删除数据库
        ///</summary>
        int Delete(int? id);
        /// <summary>
        /// 读取所有记录
        /// </summary>
        IList<Category> GetAllCategory();
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        IList<Category> GetAllCategory(object objQuery);
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录(先查所有再用List手动处理分页)
        /// </summary>
        IList<Category> GetAllCategory(object objQuery, PageStructure page);
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		IList<Category> GetAllCategoryByPage(Hashtable table, PageStructure page);
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        int GetAllCount(Hashtable table);
		
    }
}
namespace CXedu.Business.Default
{
    ///<summary>
    ///
    ///</summary>
    public class CategoryBusiness : BusinessBase<Category, int?>, ICategoryBusiness
    {
	
        ///<summary>
        ///根据ID值读取Category实体
        ///</summary>
        public Category GetById(int? id)
        {
            if(id == null) 
                throw new BusinessException("Invalid_Id_Value");
            Category entity = GetById("Category.Category_GetById", id);
            return entity;
        }
        ///<summary>
        ///根据Category实体插入数据库
        ///</summary>
        public object Insert(Category entity)
        {
            if (entity.Enable == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Insert("Category.Category_Insert", entity);
        }
        ///<summary>
        ///根据Category实体更新数据库
        ///</summary>
        public int Update(Category entity)
        {
            if (entity.Enable == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Update("Category.Category_Update", entity);
        }
        ///<summary>
        ///根据Hashtable实体更新数据库(只更新指定字段)
        ///</summary>
        public int UpdateSomeVal(Hashtable entity)
        {
            return Update("Category.Category_UpdateSomeVal", entity);
        }
        ///<summary>
        ///根据Category实体删除数据库
        ///</summary>
        public int Delete(Category entity)
        {
            if (entity.Enable == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Delete("Category.Category_Delete", entity);
        }
        ///<summary>
        ///根据Category实体ID删除数据库
        ///</summary>
        public int Delete(int? id)
        {
            if (id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            Category entity = new Category { Enable = id };
            entity.Enable = id;
            return Delete("Category.Category_Delete", entity);
        }
        /// <summary>
        /// 读取所有记录
        /// </summary>
        public IList<Category> GetAllCategory()
        {
            return GetAllCategory(null);
        }
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public IList<Category> GetAllCategory(object objQuery)
        {
            return GetAllCategory(objQuery, null);
        }
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录
        /// </summary>
        public IList<Category> GetAllCategory(object objQuery, PageStructure page)
        {
            return QueryForList("Category.Category_GetAll", objQuery, page);
        }
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		public IList<Category> GetAllCategoryByPage(Hashtable table, PageStructure page)
        {
			if (page.CurrPage == 0 || page.PageSize == 0)
			{
				return new List<Category>();
			}
			else if (page.CurrPage == 1)
			{
            	table.Add("TopNum", page.PageSize);
				return GetAllCategory(table);
			}
			
			page.Counts = GetAllCount(table);
            page.PageCount = (page.Counts % page.PageSize == 0) ? page.Counts / page.PageSize : page.Counts / page.PageSize + 1;
			
			int max = (page.CurrPage - 1) * page.PageSize;
			if (max < 500)
            {
            	table.Add("maxsize", max);
				table.Add("pagesize", page.PageSize);
				return QueryForList("Category.Category_GetByPageNotIn", table, null);
            }
			else
			{
				table.Add("pagesize", page.Counts - max > page.PageSize ? page.PageSize : page.Counts - max);
				table.Add("currcount", page.CurrPage * page.PageSize);
				if (table.ContainsKey("OrderDirection"))
                {
                    table["OrderDirection"] = table["OrderDirection"].ToString().ToUpper();
                }
				return QueryForList("Category.Category_GetByPage3Top", table, null);
			}
        }
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public int GetAllCount(Hashtable table)
        {
            return Convert.ToInt32(QueryForScalar("Category.Category_GetAllCount", table));
        }
    }
}
